package com.study.leetcode;

public class Solution_202 {
    public static boolean isHappy(int n) {
        int fast = n;
        int slow = n;
        do {
            slow = doSquare(slow);
            fast = doSquare(fast);
            fast = doSquare(fast);
        } while (fast != slow);
        return slow == 1;
    }

    private static int doSquare(int n) {
        int s = 0;
        int b;
        while (n > 0) {
            b = n % 10;
            s += b * b;
            n = n / 10;
        }
        return s;
    }

    public static void main(String[] args) {
        boolean happy = isHappy(8);
        System.out.println("happy = " + happy);
    }
}
